package com.aha.dataanalyzeandview.jsoup;

import com.aha.dataanalyzeandview.utils.ObjectUtils;
import org.apache.commons.httpclient.DefaultHttpMethodRetryHandler;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpException;
import org.apache.commons.httpclient.HttpStatus;
import org.apache.commons.httpclient.methods.GetMethod;
import org.apache.commons.httpclient.params.HttpMethodParams;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * @Program: crawl
 * @Author: 努力就是魅力
 * @Since: 2018-11-08 21:34
 * Description:
 **/

/**
 * 基金溢价查询页面解析
 */
public class ParsePremiumQueryPageHTML {


    public static void main(String[] args) {
        run();
    }

    public static void run() {

        String url = "https://www.palmmicro.com/woody/res/lofcn.php";
        // 获取tr中所有的td的数据
        List<List<String>> res = getTrTdValuesToList(url, 2);
        for (List<String> stringList : res) {
            for (String s : stringList) {
                System.out.print(s);
                System.out.print("\t");
            }
            System.out.println();
        }

        List<List<String>> res2 = getTrTdValuesToList(url, 3);
        for (List<String> stringList : res2) {
            for (String s : stringList) {
                System.out.print(s);
                System.out.print("\t");
            }
            System.out.println();
        }


    }


    /**
     * 获取表格中tr中的所有td的数据，转换成List
     *
     * @return
     */
    public static List<List<String>> getTrTdValuesToList(String url, int tableId) {
        List<List<String>> result = new ArrayList<>();
        String html = getHTMLDocument(url);

        Element tableElement = getElements(html, HTMLTag.TABLE, 0);
        Element innerTableElement = getElements(ObjectUtils.isNotNull(tableElement), HTMLTag.TABLE, tableId);
        Elements trElements  = getElements(ObjectUtils.isNotNull(innerTableElement), HTMLTag.TR);
        for (Element trElement : trElements) {
            Elements tdElements = getElements(ObjectUtils.isNotNull(trElement), HTMLTag.TD);
            List<String> tdValues = new ArrayList<>();
            for (Element tdElement : tdElements) {
                tdValues.add(tdElement.text());
            }
            result.add(tdValues);
        }

        System.out.println(result);
        return result;
    }


    /**
     * 在HTML文档中找出相应的子节点列表
     *
     * @param html
     * @param slice
     * @return
     */
    public static List<Elements> getElements(String html, String slice) {
        List<Elements> elementsList = new ArrayList<Elements>();
        Document document = Jsoup.parse(html);
        Elements elements = document.select(slice);
        for (Element e : elements) {
            elementsList.add(e.select(slice));
        }
        return elementsList;
    }

    /**
     * 在HTML文档中找出相应的子节点列表
     *
     * @param html
     * @param slice
     * @return
     */
    public static Element getElements(String html, String slice, int nodeId) {
        Document document = Jsoup.parse(html);
        Elements elements = document.select(slice);
        if(elements!=null && elements.size()>nodeId){
            return elements.get(nodeId);
        }
        return null;
    }

    /**
     * 在HTML文档中找出相应的子节点列表
     *
     * @param element
     * @param slice
     * @return
     */
    public static Element getElements(Element element, String slice, int nodeId) {
        Elements elements = element.select(slice);
        if(elements!=null && elements.size()>nodeId){
            return elements.get(nodeId);
        }
        return null;
    }

    /**
     * 在HTML文档中找出相应的子节点列表
     *
     * @param element
     * @param slice
     * @return
     */
    public static Elements getElements(Element element, String slice ) {
        Elements elements = element.select(slice);
        if(elements!=null && elements.size()>0){
            return elements;
        }
        return null;
    }




    /**
     * 获取HTML文档
     *
     * @param url 获取HTML文档的连接
     * @return
     */
    public static String getHTMLDocument(String url) {

        HttpClient httpClient = new HttpClient();
        // 设置 HTTP 连接超时 5s
        httpClient.getHttpConnectionManager().getParams().setConnectionTimeout(300 * 1000);
        // 2.生成 GetMethod 对象并设置参数
        GetMethod getMethod = new GetMethod(url);
        // 设置 get 请求超时 5s
        getMethod.getParams().setParameter(HttpMethodParams.SO_TIMEOUT, 300 * 1000);
        // 设置请求重试处理
        getMethod.getParams().setParameter(HttpMethodParams.RETRY_HANDLER, new DefaultHttpMethodRetryHandler());
        // 3.执行 HTTP GET 请求
        try {
            int statusCode = httpClient.executeMethod(getMethod);
            // 判断访问的状态码
            if (statusCode != HttpStatus.SC_OK) {
                System.err.println("Method failed: " + getMethod.getStatusLine());
            }
            // 4.处理 HTTP 响应内容
            byte[] responseBody = getMethod.getResponseBody();// 读取为字节 数组
//            System.out.println(new String(responseBody));   // 返回的是字节数组组成HTML内容
            // 返回的内容的类型  text/html; charset=utf-8
            String contentType = getMethod.getResponseHeader("Content-Type").getValue(); // 得到当前返回类型 text/html; charset=utf-8
//            System.out.println(contentType);

            return new String(responseBody);

        } catch (HttpException e) {
            // 发生致命的异常，可能是协议不对或者返回的内容有问题
            System.out.println("Please check your provided http address!");
            e.printStackTrace();
            return "";
        } catch (IOException e) {
            // 发生网络异常
            e.printStackTrace();
            return "";
        } finally {
            // 释放连接
            getMethod.releaseConnection();
        }

    }


}
